package com.ndphu.comic.gui.fragment.book;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.ndphu.comic.R;
import com.ndphu.comic.model.Book;
import com.ndphu.comic.model.Chapter;

public class OnlineBookNavigateFragment extends Fragment {

	private ChapterNavigationListener mListener;
	private Book mBook;
	private ImageButton nextButton;
	private ImageButton prevButton;
	private Chapter mChapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_online_book_navigate, container, false);
		nextButton = (ImageButton) view.findViewById(R.id.fragment_online_book_navigate_next_chapter);
		nextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mListener != null) {
					mListener.onNextChapter();
				}
			}
		});
		prevButton = (ImageButton) view.findViewById(R.id.fragment_online_book_navigate_prev_chapter);
		prevButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mListener != null) {
					mListener.onPreviousChapter();
				}
			}
		});
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		int showNext = View.VISIBLE;
		int showPrevious = View.VISIBLE;

		if (mBook != null && mChapter != null) {
			showNext = mBook.hasNext(mChapter.getChapterUrl()) ? View.VISIBLE : View.GONE;
			showPrevious = mBook.hasPrevious(mChapter.getChapterUrl()) ? View.VISIBLE : View.GONE;
		}

		nextButton.setVisibility(showNext);
		prevButton.setVisibility(showPrevious);
	}

	public void setOnNextChapterListener(ChapterNavigationListener listener) {
		mListener = listener;
	}

	public interface ChapterNavigationListener {
		void onNextChapter();

		void onPreviousChapter();
	}

	public void setBook(Book book) {
		this.mBook = book;
	}

	public void setCurrentChapter(Chapter mChapter) {
		this.mChapter = mChapter;
	}
}
